import request from "@/utils/request";
const state = {
  companyDetail: {},
  countList: [],
  mngList: [],
  companyList: [],
  activeCompany: null
};
const mutations = {
  getCompanyDetails(state, payload) {
    state.companyDetail = payload;
  },
  // 账户列表
  accountList(state, payload) {
    state.countList = payload;
  },
  // 公司列表
  manageList(state, payload) {
    state.mngList = payload;
  },
  // 获取总公司
  allCompanyList(state, payload) {
    state.companyList = payload;
  },
  // 获取当前公司名称
  activeCompanyName(state, payload) {
    state.activeCompany = payload;
    localStorage.setItem("currentCompany", payload);
  }
};
const actions = {
  // 获取当前公司
  syncGetActiveCompany({ commit }) {
    request({ url: "/hr/api/v1/users/childCompanyName" }).then(({ data }) => {
      commit("activeCompanyName", data);
    });
  },
  // 获取总公司
  async syncGetCompany({ commit, rootState, rootGetters }) {
    const { data } = await request({
      url: "/hr/api/v1/childrenCompany/listAll",
      params: { companyId: rootState.hr.companyId }
    });
    rootGetters.permissions_site.forEach((item, index) => {
      if (item.currSite) {
        data.unshift({
          id: "supreme",
          name: item.name
        });
      }
    });
    commit("allCompanyList", data || []);
  }
};
const getters = {
  COMPANY_DETAIL: state => state.companyDetail,
  COUNTLIST: state => state.countList,
  MNGLIST: state => state.mngList,
  COMPANYLIST: state => state.companyList,
  ACTIVECOMPANYNAME: state => state.activeCompany
};
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
};
